<template>
	<view class="charts-box" @touchstart.prevent="goTouchstart(item.numId)"
		@touchend.prevent="goTouchend(item.numId,3)">
		<view class="t-switch" @touchstart.prevent="change()">
			<u-switch v-model="enable" size="12" inactiveValue="1"></u-switch>
		</view>
		<qiun-data-charts type="line" :opts="opts" :chartData="chartData" background="#f1f1f1" :ontouch="true"
			:disableScroll="false" />
	</view>

</template>

<script>
	export default {
		props: ["item", "foot"],
		watch: {
			item(n, o) {
				this.getData()
			}
		},
		created() {
			this.getData()
		},
		data() {
			return {
				dataList: [],
				opts: {},
				chartData: {},
				enable: false
			}
		},
		methods: {
			change() {
				this.opts.enableScroll = !this.opts.enableScroll
			},
			init() {
				this.opts = {
					"type": "line",
					"canvasId": this.item.numId,
					"canvas2d": true,
					"background": "#00ff7f",
					"animation": true,
					"timing": "easeOut",
					"duration": 1000,
					"restore": false,
					"color": [
						"#1890FF",
						"#91CB74",
						"#FAC858",
						"#EE6666",
						"#73C0DE",
						"#3CA272",
						"#FC8452",
						"#9A60B4",
						"#ea7ccc"
					],
					"errorReload": true,
					"fontSize": 13,
					"fontColor": "#5a5a5a",
					"enableScroll": this.enable,
					"touchMoveLimit": 60,
					"enableMarkLine": true,
					"dataLabel": true,
					"dataPointShape": true,
					"dataPointShapeType": "solid",
					"tapLegend": true,
					"xAxis": {
						"disabled": true,
						"axisLine": true,
						"axisLineColor": "#353535",
						"calibration": true,
						"fontColor": "#666666",
						"fontSize": 13,
						"rotateLabel": true,
						"itemCount": 5,
						"boundaryGap": "center",
						"disableGrid": true,
						"gridColor": "#4d4d4d",
						"gridType": "dash",
						"dashLength": 4,
						"gridEval": 1,
						"scrollShow": true,
						"scrollAlign": "left",
						"scrollColor": "#55557f",
						"scrollBackgroundColor": "#EFEBEF",
						"format": ""
					},
					"yAxis": {
						"disabled": false,
						"disableGrid": true,
						"splitNumber": 5,
						"gridType": "dash",
						"dashLength": 2,
						"gridColor": "#161616",
						"padding": 10,
						"showTitle": false,
						"data": []
					},
					"legend": {
						"show": true,
						"position": "top",
						"float": "center",
						"padding": 5,
						"margin": 5,
						"backgroundColor": "rgba(0,0,0,0)",
						"borderColor": "rgba(0,0,0,0)",
						"borderWidth": 0,
						"fontSize": 13,
						"fontColor": "#55aaff",
						"lineHeight": 11,
						"hiddenColor": "#CECECE",
						"itemGap": 10
					},
					"extra": {
						"line": {
							"type": "curve",
							"width": 2
						},
						"tooltip": {
							"showBox": true,
							"showArrow": true,
							"showCategory": false,
							"borderWidth": 0,
							"borderRadius": 0,
							"borderColor": "#000000",
							"borderOpacity": 0.7,
							"bgColor": "#000000",
							"bgOpacity": 0.7,
							"gridType": "solid",
							"dashLength": 4,
							"gridColor": "#CCCCCC",
							"fontColor": "#FFFFFF",
							"splitLine": true,
							"horizentalLine": false,
							"xAxisLabel": false,
							"yAxisLabel": false,
							"labelBgColor": "#FFFFFF",
							"labelBgOpacity": 0.7,
							"labelFontColor": "#666666"
						},
						"markLine": {
							"type": "solid",
							"dashLength": 4,
							"data": []
						}
					}
				}
				this.chartData = {
					"categories": this.dataList || [],
					"series": [{
						"name": (this.item.numName + " " + this.item.unit) || '名称',
						"data": this.dataList || []
					}],
				}
			},

			getData() {
				if (this.item.deviceId) {
					let key = this.item.deviceId + "_" + this.item.subscribe
					this.$http.request({
						url: 'app/number/keyNumber/' + key,
						method: 'GET',
						success: (data) => {
							if (data.data && data.data.code == 0) {
								this.dataList = data.data.listData
								this.init()
							}
						}
					})
				}
			},
			// 按键长按
			goTouchstart(numId) {
				let _this = this;
				clearTimeout(_this.timeOutEvent);
				_this.timeOutEvent = setTimeout(function() {
					_this.timeOutEvent = 0;
					//  处理长按事件...
					_this.$emit('go-touchstart', numId)
				}, 600);
			},
			// 按钮单击
			goTouchend(numId, type) {
				if (this.foot == null) {
					return;
				}
				if (!this.foot) {

				}
				if (this.foot) {
					uni.navigateTo({
						url: '../components/infi-comp?type=' + type + "&id=" + numId
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.charts-box {
		width: 100%;
		height: 220px;
		z-index: 0;
	}

	.t-switch {
		position: absolute;
		margin-left: 660rpx;
		margin-top: 45rpx;
		z-index: 1;
	}
</style>
